//
//  RootViewController.m
//  CarStop
//
//  Created by lanou3g on 15/7/20.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "RootViewController.h"
#import "NewListTableViewController.h"
#import "MationListTableViewController.h"

@interface RootViewController (){

    UISegmentedControl *segment;
    NewListTableViewController *newVC;
    MationListTableViewController *newestVC;

}

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    segment = [[UISegmentedControl alloc] initWithItems:@[@"新闻",@"最新"]];
    segment.frame = CGRectMake(0, 6, 90, 30);
    //居中放置分段控件到导航条    [self.navigationController.navigationBar.topItem setTitleView:segment];
    UIBarButtonItem *leftListBarButton = [[UIBarButtonItem alloc] initWithCustomView:segment];
    self.navigationItem.leftBarButtonItem = leftListBarButton;
    [segment addTarget:self action:@selector(changeView:) forControlEvents:UIControlEventValueChanged];
    
    newVC = [[NewListTableViewController alloc] init];
    [self addChildViewController:newVC];
    [self.view addSubview:newVC.view];
    
    
    newestVC = [[MationListTableViewController alloc] init];
    [self addChildViewController:newestVC];
    [self.view addSubview:newestVC.view];
    
    
}


- (void)changeView:(UISegmentedControl *)segmentControl{
    
    switch (segmentControl.selectedSegmentIndex) {
        case 0:
            [self.view bringSubviewToFront:newVC.view];
            segment.selectedSegmentIndex = 0;
            [self.view bringSubviewToFront:segment];
            self.title = @"新闻";
            break;
        case 1:
            [self.view bringSubviewToFront:newestVC.view];
            self.title = @"最新";
            break;
            
        default:
            break;
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
